var express = require('express');
var router = express.Router();
var { ruser_model } = require("../utils/model")
var { createToken, checkToken } = require("../utils/token")

//注册
router.post("/register", (req, res) => {
    var body = req.body
    ruser_model.findOne({
        phone: body.phone
    })
        .then(result => {
            if (result) {
                res.json({
                    code: 401,
                    msg: "当前的手机号码已经被注册",
                    result
                })
            } else {
                body.time = new Date();
                ruser_model.insertMany(body)
                    .then(data => {
                        res.json({
                            code: 200,
                            msg: "成功",
                            result: data
                        })
                    })
            }
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})
// 登录
router.post("/login", (req, res) => {
    var body = req.body
    ruser_model.findOne({
        account: body.account,
    })
        .then(result => {
            if (result) {
                if (result.password == body.password) {
                    body.phone = result.phone;
                    var token = createToken(body)
                    res.json({
                        code: 200,
                        msg: "登录成功",
                        result,
                        token
                    })
                } else {
                    res.json({
                        code: 401,
                        msg: "登录失败",
                        result
                    })
                }
            } else {
                res.json({
                    code: 401,
                    msg: "当前用户未注册,请联系管理员或者注册",
                    result
                })
            }
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

// 获取用户信息
router.get("/getuserinfo", (req, res) => {
    checkToken(req, res, ({ phone }) => {
        qfuser_model.findOne({
            phone
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取用户信息成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})


// 上传文件
var multer = require("multer")
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './public/avatar')
    },
    filename: function (req, file, cb) {
        cb(null, 'WH2105-' + Date.now() + file.originalname)
    }
})

var upload = multer({ storage: storage }).any()
router.post("/uploadavatar", upload, (req, res) => {
    console.log(req.files)
    var path = req.files[0].path
    checkToken(req, res, ({ phone }) => {
        qfuser_model.updateMany({
            phone
        }, {
            $set: {
                avatar: path
            }
        }).then(result => {
            res.json({
                code: 200,
                msg: "上传成功",
                path: path,
                result
            })
        })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

router.post("/uploadimg", upload, (req, res) => {
    var path = req.files[0].path
    checkToken(req, res, ({ phone }) => {
        res.json({
            code: 200,
            msg: "上传成功",
            path: path,
        })
    })
})

module.exports = router